/**
 * 详情对话框
 */
var StoragePutSingleInfoDlg = {
    data: {
    	positionNumber: "",
    }
};
/**
 * 添加或者修改页面
 */
var OrderPutDetailedInfoDlg = {
    data: {
        detailedGoodsname: "",
        detailedGoodsnum: "",
        goodsNumber: "",
		goodsNo:"",
        detailedRebubbles: "",
        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        detailedPutcondition: "",
        detailedPiecenum: "",
        goodsWeight: "",
        goodsVolume: "",
    }
};
layui.use(['table', 'admin', 'ax','laydate','form','element'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;
	var element = layui.element
	
    laydate.render({
      elem: '#startTime3'
    });

    laydate.render({
      elem: '#endTime3'
    });
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})
	
    /**
     * 库存表管理
     */
    var StorageStock = {
        tableId: "storageStockTable"
    };
	
    /**
     * 初始化表格的列
     */
    StorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'stockstate', title: 'Status',templet: '#stockstateTpl'},
            {field: 'operateName', title: 'Project'},
            {field: 'customerName', title: 'Customer'},
            {field: 'warehouseName', title: 'Warehouse'},
            {field: 'goodsName', title: 'Product', style:'background-color: #dbf1e1;font-weight: 600;',width:270},
            {field: 'goodsNumber', title: 'Code', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', title: 'Item No', sort: true, style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:', event: 'checkbox'},
            {field: 'num', title: 'Quantity', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'producetime', title: 'Production Date'},
            {field: 'stockWeight', title: 'Weight', totalRow: true},
            {field: 'stockVolume', title: 'Volume', totalRow: true},
            {field: 'stockMoney', title: 'Amount', totalRow: true},
            {field: 'unitprice', title: 'Unit Price'},
            {field: 'goodsBigtype', title: 'Category'},
            {field: 'goodsType', title: 'Type', hide: true},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsPacking', title: 'Packing'},
            {field: 'goodsSpecs', title: 'Specs'},
            {field: 'goodsWarranty', title: 'Shelf Life (days)'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    StorageStock.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        table.reload(StorageStock.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    StorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(StorageStock.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StorageStock.tableId,
        url: Feng.ctxPath + '/storageStock/stockListProducetime',
        page: true,
        height: "full-60",
        limit: 60,
		limits:[60,120,240,360,480,600,1500,5000,10000],
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StorageStock.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
    	StorageStock.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
    	StorageStock.exportExcel();
    });
    
    //点击选择商品名称
    $('#choiceg').click(function () {
    	var customerId = $("#customerId").val();
    	if (customerId==null||customerId=='') {
    		layer.msg('Please select customer'); 
		} else {
	        layer.open({
	            type: 2,
	            title: 'Select Product',
	            area: ['900px', '500px'],
	            content: Feng.ctxPath + '/orderPutDetailed/toChoiceGoods?goodsCustomerId=' + customerId,
	            end: function () {
	                $("#goodsName").val(OrderPutDetailedInfoDlg.data.detailedGoodsname);//商品名称
	                $("#goodsNumber").val(OrderPutDetailedInfoDlg.data.goodsNumber);//商品编号
	                $("#goodsNo").val(OrderPutDetailedInfoDlg.data.goodsNo);//货号
	            }
	        });
		}
    });
    
    //查询商品类别
    form.on('select(goodsBigtype)',function(){
    	$("#goodsType").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/storageStock/queryGoodsType',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"dictcode":$("#goodsBigtype").find("option:checked").attr("id"),"code":"COMMODITY_CATEGORY"},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>Please select</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.name+"'>"+value.name+"</option>";
    	        	});
    	        	$("#goodsType").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });

});
